<template>
  <div class='app-container'>
    <!-- 搜索表单 -->
    <el-form ref='queryForm' :model='queryParams' size='small' :inline='true'>
      <el-form-item>
        <el-button
          v-has-permission="['sys:generator:add']"
          type='primary'
          icon='el-icon-plus'
          :disabled='!tableNames.length > 0'
          @click='handleImport'>批量导入
        </el-button>
        <el-button
          v-has-permission="['sys:generator:edit']"
          type="warning"
          icon="el-icon-setting"
          @click="handleConfig"
        >导入设置
        </el-button>
      </el-form-item>
      <el-form-item prop='tableName'>
        <el-input
          v-model='queryParams.tableName'
          placeholder='数据表名'
          clearable
          style='width: 240px'
          @keyup.enter.native='handleQuery'
        />
      </el-form-item>
      <el-form-item>
        <el-button type='primary' icon='el-icon-search' @click='handleQuery'>搜索</el-button>
        <el-button icon='el-icon-refresh' @click='handleReset'>重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table
      v-loading='loading'
      :data='pageList'
      border
      @selection-change='handleSelectionChange'
    >
      <el-table-column type='selection' width='55' align='center' />
      <el-table-column label='表名' prop='tableName' width='200' />
      <el-table-column label='描述' prop='tableComment' />
      <el-table-column label='创建时间' prop='createTime' width='200' />
      <el-table-column label='操作' align='center' width='400'>
        <template slot-scope='scope'>
          <el-button
            v-has-permission="['sys:generator:import']"
            type='primary'
            size='mini'
            round
            plain
            @click.stop='handleImport(scope.row)'>导入数据表</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show='pagination.total > 0'
      :total='pagination.total'
      :page.sync='pagination.page'
      :limit.sync='pagination.limit'
      @pagination='handleQuery'
    />
    <!-- 参数配置弹窗 -->
    <el-dialog
      :title="dialog.title"
      :visible.sync="dialog.configVisible"
      width="700px"
    >
      <el-form
        ref="genConfigForm"
        :model="genConfig"
        :rules="genConfigRules"
        label-width="100px"
      >
        <el-form-item label="作者" prop="author">
          <el-input v-model="genConfig.author" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="模块名称" prop="moduleName">
          <el-input v-model="genConfig.moduleName" placeholder="请输入模块名称" />
        </el-form-item>
        <el-form-item label="生成包路径" prop="packageName">
          <el-input v-model="genConfig.packageName" placeholder="请输入生成包路径" />
        </el-form-item>
        <el-form-item label="去除表前缀" prop="autoRemovePre">
          <el-radio-group v-model="genConfig.autoRemovePre">
            <el-radio :label="true" border>是</el-radio>
            <el-radio :label="false" border>否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="表前缀" prop="tablePrefix">
          <el-input v-model="genConfig.tablePrefix" placeholder="请输入生成包路径" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="genConfigSubmit">确 定</el-button>
        <el-button @click="dialog.configVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { configAdd, configInfo, dbList, importTableSave } from '@/api/admin/generator'

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      tableNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      queryParams: {
        tableName: undefined
      },
      pagination: {
        page: 1,
        limit: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false,
        type: undefined, // type 操作类型：1-新增 2-修改
        configVisible: false
      },
      genConfig: {
        autoRemovePre: false,
        moduleName: 'system',
        packageName: 'com.samphanie.auiu',
        tablePrefix: 'sys_',
        author: 'dries'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      genConfigRules: {}
    }
  },
  async created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = this.pagination.page
      this.queryParams.pageSize = this.pagination.limit
      dbList(this.queryParams).then(response => {
        const { data } = response
        this.pageList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    handleReset() {
      this.pagination = {
        page: 1,
        limit: 10,
        total: 0
      }
      this.queryParams = {
        tableName: undefined
      }
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.tableNames = selection.map(item => item.tableName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleConfig() {
      this.resetGenConfigForm()
      this.dialog = {
        title: '默认设置',
        configVisible: true
      }
      configInfo().then((res) => {
        if (res.code === 20000 && res.data) {
          const { data } = res
          this.genConfig = data
        }
      })
    },
    handleImport(row) {
      const data = row.tableName ? [row.tableName] : this.tableNames
      if (data.length <= 0) {
        return
      }
      importTableSave(data).then(res => {
        if (res && res.code === 20000) {
          this.$message.success('导入表结构成功')
        } else {
          this.$message.error('导入失败')
        }
      })
      this.handleQuery()
    },
    genConfigSubmit() {
      configAdd(this.genConfig).then((res) => {
        this.$message.success('提交成功')
        this.closeDialog()
        this.handleQuery()
      })
    },
    resetForm() {
      this.form = {}
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    resetGenConfigForm() {
      this.genConfig = {
        autoRemovePre: false,
        moduleName: 'system',
        packageName: 'com.samphanie.auiu',
        tablePrefix: 'sys_',
        author: 'dries'
      }
    },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined,
        configVisible: false
      }
    }
  }
}
</script>
